<%-- 
    Document   : game
    Created on : Mar 15, 2013, 2:40:24 PM
    Author     : Administrator
--%>

<%@page import="lucky9.Game"%>
<%@page import="lucky9.Player"%>
<%@page contentType="text/html" pageEncoding="Shift_JIS"%>
<%
  Game game = (Game) application.getAttribute("GAME");
  if (game == null) {
    game = new Game();
    application.setAttribute("GAME", game);
  }
  String ip = request.getRemoteAddr();
  String name = request.getParameter("name");
  Player me = null;
  if (name != null && name.length() > 0) {
    if (game.getPlayerMap().containsKey(ip)) {
      me = game.getPlayerMap().get(ip);
      me.setName(name);
    } else {
      if (game.getPlayers().size() < 13) {
        me = new Player(name, ip);
        game.joinPlayer(me);
      }
    }
    session.setAttribute("PLAYER", me);
  } else {
    return;
  }

%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
    <title>Lucky 9</title>
    <script src="jquery.js" ></script>
    <script>
      var promptOpened = false;
      function askBet(){
        if(promptOpened == false){
          promptOpened = true;
          var value = prompt("Bet: ");
          $.post("command", {
            command: "bet",
            bet: value}, function(data) {
          });
        }
      }
      $(document).ready(function() {
        setInterval(function() {
          $.get("game.jsp", function(data) {
            $('#gameState').html(data);
          });
        }, 1000);

        $('#start-game').click(function() {
          $.post("command", {
            command: "start"
          }, function(data) {
          });
        });

        $('#hit-me').click(function() {
          $.post("command", {
            command: "hitme"
          }, function(data) {
          });
        });
        $('#stand').click(function() {
          $.post("command", {
            command: "stand"
          }, function(data) {
          });
        });
        $('.set-banker').live("click", function() {
          var btn = $(this);
          $.post("command", {
            command: "set-banker",
            ip: btn.data("ip")
          }, function(data) {
          });
        });
      });

    </script>
    <link href="index.css" rel="stylesheet">
    <link href="cards.css" rel="stylesheet">
  </head>
  <body>
    <div id="controls">
      <button id="start-game" >Start Game</button>
<!--      <button id="cash-out" style="margin-right:100px;">Cash Out</button>-->
      <button id="hit-me" style="margin:0px 100px;">Hit Me!</button>
      <button id="stand" >Stand</button>
    </div>
    <div id="gameState"></div>
  </body>
</html>
